package com.museum.controller.impl.feign;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.museum.base.BaseApiService;
import com.museum.base.BaseResponse;
import com.museum.controller.feignAPI.FeignController;
import com.museum.core.utils.RedisUtil;
import com.museum.entity.DO.Area;
import com.museum.entity.DO.Token;
import com.museum.entity.DTO.RedisInfo.UserMessage;
import com.museum.entity.DTO.feign.UserToClue;
import com.museum.mapper.AreaMapper;
import com.museum.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @ClassName museum
 * @Author: 王瑞文
 * @Date: 2021/1/6 14:24
 */
@RestController
public class FeignControllerImpl extends BaseApiService<JSONObject> implements FeignController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public BaseResponse<JSONObject> getUserMessageByUSerId(String token) {
        System.out.println(token);
        String value = redisUtil.getString(token);
        UserMessage userMessage = JSONObject.parseObject(value, UserMessage.class);
        Integer userID = userMessage.getUserId();
        Integer areaNumber = userMessage.getAreaNumber();
        String realName = userMapper.selectRealName(userID);
        if (StringUtils.isEmpty(realName)) return setResultNotFound("查询结果为空");
        JSONObject object = new JSONObject();
        object.put("realName", realName);
        object.put("areaNumber", areaNumber);
        return setResultSuccess(object);
    }

    public List<Integer> findFatherNumber(List<Integer> fatherIds, Integer areaNumber) {
        fatherIds.add(areaNumber);
        Integer fatherNumber = areaMapper.findFatherNode(areaNumber);
        Area area = areaMapper.selectById(fatherNumber);
        if (area.getParentId() != 0) {
            if (area.getParentId() != 215)
                findFatherNumber(fatherIds, fatherNumber);
            else fatherIds.add(areaMapper.findFatherNode(fatherNumber));
        }
        fatherIds.add(fatherNumber);
        return fatherIds;
    }

    public List<String> findFatherName(List<String> fatherNames, List<Integer> fatherIds, Integer areaNumber) {
        Area area1 = areaMapper.selectOne(new QueryWrapper<Area>().eq("area_number", areaNumber));
        fatherNames.add(area1.getAreaName());
        Integer fatherNumber = areaMapper.findFatherNode(areaNumber);
        Area area = areaMapper.selectById(fatherNumber);
        fatherNames.add(areaMapper.selectOne(new QueryWrapper<Area>().eq("area_number", fatherNumber)).getAreaName());
        if (area.getParentId() != 0) {
            if (area.getParentId() != 215)
                findFatherName(fatherNames, fatherIds, fatherNumber);
            else fatherNames.add(areaMapper.selectById(area.getParentId()).getAreaName());
        }

        return fatherNames;
    }

    @Override
    public BaseResponse<JSONObject> getAreaTreeByNodeId(String token) {
        return null;
    }
}
